package com.ruoyi.web.controller.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.TypeConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.BsJingqu;
import com.ruoyi.system.domain.ro.index.*;
import com.ruoyi.system.service.BsJingquService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.bytedeco.javacpp.annotation.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.java2d.pipe.AAShapePipe;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName IndexController
 * @Description 描述
 * @Author HanWL
 * @Date 2023/11/13 10:55
 * @Version 1.0
 **/
@RestController
@RequestMapping("/bs/index")
@Api(tags = "首页接口")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class IndexController {

    @Resource
    private BsJingquService bsJingquService;

    @GetMapping("/people")
    @ApiOperation("首页上面展示的今日入园，累计入园及明日预约人数")
    public AjaxResult<PeopleNum> people(){
        PeopleNum peopleNum = bsJingquService.getPeopleNum();
        return AjaxResult.success(peopleNum);
    }

    @GetMapping("/jinqu/people")
    @ApiOperation("获取景区人流排名")
    @ApiParam(value = "limit", name = "限制展示多少条")
    public AjaxResult<?> jingquPeople(Integer limit){
        List<BsJingqu> jingquList = bsJingquService.list();
        Map<Long, String> jignquMap = jingquList.stream().collect(Collectors.toMap(
                BsJingqu::getJingquId,
                BsJingqu::getJingquName,
                (a, b) -> b
        ));
        List<JingquPeopleNum> list = bsJingquService.getJingquPeople(limit);
        list.forEach(x -> {
            x.setJingquName(jignquMap.getOrDefault(x.getJingquId(),""));
            if (x.getNum().doubleValue() > 10000.0) {
                x.setNum(x.getNum().divide(BigDecimal.valueOf(10000),2, RoundingMode.HALF_UP));
                x.setUnit("万人");
            } else {
                x.setUnit("人");
            }

        });
        return AjaxResult.success(list);
    }

    @GetMapping("/city/people")
    @ApiOperation("客源地统计")
    public AjaxResult<?> cityPeople(){
        List<CityPeopleNum> cityPeopleNum = bsJingquService.getCityPeopleNum();
        List<CityPeopleNum> result = bsJingquService.dealWithCity(cityPeopleNum);
        return AjaxResult.success(result);
    }

    @GetMapping("/age/people")
    @ApiOperation("获取不同年龄的人占比")
    public AjaxResult<?> agePeople(){
        List<CityPeopleNum> cityPeopleNum = bsJingquService.getCityPeopleNum();
        List<AgePeopleNum> result = bsJingquService.dealWithAge(cityPeopleNum);
        return AjaxResult.success(result);
    }

    @GetMapping("/time/people")
    @ApiOperation("获取不同时间段的游客数量情况")
    public AjaxResult<?> timePeople(){
        List<TimePeopleNum> timePeopleNums = bsJingquService.getTimePeopleNum();
        return AjaxResult.success(timePeopleNums);
    }

    @ApiOperation("客流检测")
    @GetMapping("/analysis/people")
    public AjaxResult<?> analysisPeople(){
        Date now = new Date();
        int todayNum = bsJingquService.getPeopleNumForDay(now);
        Date yesterday = DateUtils.addDays(now, -1);
        int yesterdayNum = bsJingquService.getPeopleNumForDay(yesterday);
        int lastDayNum = bsJingquService.getPeopleNumForDay(DateUtils.addDays(now, -2));

        int lastYearToday = bsJingquService.getPeopleNumForDay(DateUtils.addYears(now, -1));
        int lastYearYesterday = bsJingquService.getPeopleNumForDay(DateUtils.addYears(yesterday, -1));
        Map<String,Object> map = new HashMap<>();
        map.put("todayNum",todayNum);
        map.put("yesterdayNum",yesterdayNum);

        // 今日环比
        double todayValue = yesterdayNum == 0 ? 0 : ((todayNum - yesterdayNum) / (double) yesterdayNum) * 100;
        String todayCircle =  String.format("%.2f", Math.abs(todayValue)) + "%";
        map.put("todayCircle", todayCircle);
        map.put("todayCircleD", todayValue < 0 ? "down" : "up");
        // 昨日环比
        double yesterdayValue = lastDayNum == 0 ? 0 : ((yesterdayNum - lastDayNum) / (double)lastDayNum) * 100;
        String yesterdayCircle = String.format("%.2f", Math.abs(yesterdayValue)) + "%";
        map.put("yesterdayCircle", yesterdayCircle);
        map.put("yesterdayCircleD", yesterdayValue < 0 ? "down" : "up");
        // 今日同比
        double todaySyncValue = lastYearToday == 0 ? 0 : ((todayNum - lastYearToday) / (double)lastYearToday) * 100;
        String todaySync = String.format("%.2f", Math.abs(todaySyncValue)) + "%";
        map.put("todaySync", todaySync);
        map.put("todaySyncD", todaySyncValue < 0 ? "down" : "up");
        // 昨日同比
        double yesterdaySyncValue = lastYearYesterday == 0 ? 0 : ((yesterdayNum - lastYearYesterday) / (double)lastYearYesterday) * 100;
        String yesterdaySync = String.format("%.2f", Math.abs(yesterdaySyncValue)) + "%";
        map.put("yesterdaySync", yesterdaySync);
        map.put("yesterdaySyncD", yesterdaySyncValue < 0 ? "down" : "up");
        return AjaxResult.success(map);
    }

    @ApiOperation("设备统计")
    @GetMapping("/equip")
    public AjaxResult<?> equip(){
        List<EquipNum> equipNums = new ArrayList<>();
        equipNums.add(new EquipNum("视频监控",213,11,0));
        equipNums.add(new EquipNum("消防设备",534,0,0));
        equipNums.add(new EquipNum("闸机设备",21,2,1));
        return AjaxResult.success(equipNums);
    }

    @Deprecated
    @ApiOperation("(已弃用，正式版本上线后删除)首页-导航接口")
    @GetMapping("router")
    public AjaxResult<?> navigation(){
        LambdaQueryWrapper<BsJingqu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BsJingqu::getDelFlag, Constants.LOGIC_NOT_DELETE);
        List<BsJingqu> jingquList = bsJingquService.list(lqw);
        List<Map<String,Object>> result = bsJingquService.toTree(jingquList);
        return AjaxResult.success(result);
    }

    @Deprecated
    @GetMapping("getInfo")
    @ApiOperation("(已弃用，正式版本上线后删除)首页-信息接口")
    public AjaxResult<?> getInfo(Long id){
        Map<String,Object> resultMap = bsJingquService.getInfo(id);
        return AjaxResult.success(resultMap);
    }

    @Deprecated
    @ApiOperation("(已弃用，正式版本上线后删除)首页-流量数据对比")
    @GetMapping("getSituation")
    public AjaxResult<?> getSituation(Long id){
        Map<String,Object> resultMap = bsJingquService.getSituation(id);
        return AjaxResult.success(resultMap);
    }

    @Deprecated
    @ApiOperation("(已弃用，正式版本上线后删除)首页-获取景区不同时间段的人数")
    @GetMapping("getPeopleNum")
    public AjaxResult<?> getPeopleNum(Long id){

        return AjaxResult.success();
    }

    @Deprecated
    @ApiOperation("(已弃用，正式版本上线后删除)首页-获取不同年龄段占比")
    @GetMapping("getAgeNum")
    public AjaxResult<?> getAgeNum(Long jingquId){

        return AjaxResult.success();
    }
}
